package com.tool.engine.export.mertics.system;

import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.function.UnaryOperator;

@Data
public class MemoryUsage {
    private double maxUsage;

    private double minUsage;

    private double usedUsage;

    public MemoryUsage(double maxUsage, double minUsage, double usedUsage) {
        this.maxUsage = maxUsage;
        this.minUsage = minUsage;
        this.usedUsage = usedUsage;
    }

    public MemoryUsage() {
        this.maxUsage = 0;
        this.minUsage = 1.0;
        this.usedUsage = 0;
    }

    public UnaryOperator<MemoryUsage> createFunction(long usedMemorySize, long totalMemorySize) {
        return old->{
            if(Objects.isNull(old)){
                old = new MemoryUsage();
            }
            double used = new BigDecimal(usedMemorySize).divide(new BigDecimal(totalMemorySize), 2, RoundingMode.HALF_UP).doubleValue();
            double max = Math.max(old.getMaxUsage(), used);
            double min = Math.min(old.getMinUsage(), used);
            return new MemoryUsage(max,min, used);
        };

    }

}
